package com.xiaoyang.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoyang.forum.common.code.Result;
import com.xiaoyang.forum.common.code.ResultCode;
import com.xiaoyang.forum.common.exception.ApplicationException;
import com.xiaoyang.forum.domain.pojo.User;
import com.xiaoyang.forum.domain.pojo.UserFollow;
import com.xiaoyang.forum.domain.vo.UserFollowVO;
import com.xiaoyang.forum.mapper.UserFollowMapper;
import com.xiaoyang.forum.mapper.UserMapper;
import com.xiaoyang.forum.service.UserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author XiaoYang
 * @since 2025-09-17
 */
@Slf4j
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Autowired
    private UserFollowMapper userFollowMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 关注用户
     * @param followUserId  关注者id
     * @param followedUserId 被关注者id
     * @return
     */
    @Override
    public int follow(long followUserId, long followedUserId) {
        log.info("关注者: {}, 被关注者: {}", followUserId, followedUserId);

        // 两个用户是否存在
        User followUser = userMapper.selectById(followUserId);
        User followedUser = userMapper.selectById(followedUserId);
        if (followUser == null || followedUser == null) {
            log.error("用户不存在");
            throw new ApplicationException(Result.failed(ResultCode.USER_NO_LOGIN));
        }

        // 不能关注自己
        if (followUserId == followedUserId) {
            log.error("不能关注自己");
            throw new ApplicationException(Result.failed(ResultCode.FAILED_CANNOT_FOLLOW_SELF));
        }

        // 根据关注者id和被关注者id查询记录是否存在
        UserFollow isUserFollow = userFollowMapper.selectOne(new QueryWrapper<UserFollow>()
                .eq("follow_user_id", followUserId)
                .eq("followed_user_id", followedUserId));

        long userId = followedUserId;
        User user = userMapper.selectById(userId);

        if (isUserFollow != null) {
            // 已关注 取消关注
            // todo 修改用户表的粉丝量
            user.setFans(user.getFans() - 1);
            userMapper.updateById(user);
            return userFollowMapper.deleteById(isUserFollow);
        } else {
            // 未关注 关注
            UserFollow userFollow = new UserFollow();
            userFollow.setFollowUserId(followUserId);
            userFollow.setFollowedUserId(followedUserId);
            userFollow.setState(0);
            userFollow.setCreateTime(LocalDateTime.now());
            userFollow.setUpdateTime(LocalDateTime.now());

            // todo 修改用户表的粉丝量
            user.setFans(user.getFans() + 1);
            userMapper.updateById(user);
            return userFollowMapper.insert(userFollow);
        }
    }

    /**
     * 获取已关注的用户列表
     * @param followUserId
     * @return
     */
    @Override
    public List<UserFollowVO> getFollowedUserList(long followUserId) {
        User user = userMapper.selectById(followUserId);
        if (user == null) {
            log.error("用户不存在");
            throw new ApplicationException(Result.failed(ResultCode.USER_NO_LOGIN));
        }
        return userFollowMapper.getFollowedUserList(followUserId);
    }

    /**
     * 获取粉丝列表
     * @param followedUserId
     * @return
     */
    @Override
    public List<UserFollowVO> getFollowUserList(long followedUserId) {
        User user = userMapper.selectById(followedUserId);
        if (user == null) {
            log.error("用户不存在");
            throw new ApplicationException(Result.failed(ResultCode.USER_NO_LOGIN));
        }
        return userFollowMapper.getFollowUserList(followedUserId);
    }
}
